layui.use(['layer', 'form', 'table', 'ztree', 'laydate', 'admin', 'ax'], function () {
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var laydate = layui.laydate;
    var admin = layui.admin;

    /**
     * 系统管理--用户管理
     */
    var userProfit = {
        tableId: "userProfitTable",    //表格id
        condition: {
            name: "",
            deptId: "",
            timeLimit: ""
        }
    };

    /**
     * 初始化表格的列
     */
    userProfit.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'user_profit_id', hide: true, sort: true, title: 'userProfitId'},
            {field: 'user_id', hide: true, sort: true, title: 'user_id'},
            {field: 'school_name', sort: true, title: '学校名称'},
            {field: 'school_code', sort: true, title: '学校代码'},
            {field: 'user_profit_all', sort: true, title: '考务总收益'},
            {field: 'user_profit_all', sort: true, title: '考点总收益'},
            {field: 'user_profit_rest', sort: true, title: '剩余提现金额'},
            {field: 'user_profit_extract', sort: true, title: '已提现总金额'},
            {field: 'user_name', sort: true, title: '转账人姓名'},
            {field: 'user_ali_account', sort: true, title: '支付宝账号'},
            {field: 'user_profit_rest', sort: true, title: '已提现总金额'},
            {field: 'crea_time', sort: true, title: '创建时间'},
            {field: 'state', sort: true, title: '考试内容' ,templet: function (d) {
                    if(d.state == 0){return '待提现';}
                    else if(d.state == 1){return '审核中';}else if(d.state == 2){return '打款中';}
                    else if(d.state == 3){return '已提现';}else if(d.state == -1){return '拒绝';}
                }},
            {field: 'remarks', sort: true, title: '备注'},
            {align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 230}
        ]];
    };

    /**
     * 点击查询按钮
     */
    userProfit.search = function () {
        var queryData = {};
        queryData['schoolName'] = $("#schoolName").val();
        queryData['state'] = $("#state").val();
        table.reload(userProfit.tableId, {where: queryData});
    };

    /**
     * 导出excel按钮
     */
    userProfit.exportExcel = function () {
        var checkRows = table.checkStatus(userProfit.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑用户按钮时
     *
     * @param data 点击按钮时候的行数据
     */
    userProfit.onExtractExam = function (data) {
        admin.putTempData('formOk', false);
        layui.admin.open({
            type: 2,
            title: '提现',
            area: ['70%', '80%'],
            content: Feng.ctxPath + '/exam/userProfit_extract?userProfitId=' + data.user_profit_id ,
            end: function () {
                admin.getTempData('formOk') && table.reload(userProfit.tableId);
            }
        })
    };

    userProfit.onTransferExam = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/exam/extractProfit", function () {
                table.reload(userProfit.tableId);
                Feng.success("成功!");
            }, function (data) {
                Feng.error("失败!" + data.responseJSON.message + "!");
            });
            ajax.set("userProfitId", data.user_profit_id);
            ajax.set("state", 3);
            ajax.start();
        };
        Feng.confirm("是否打款给" + data.user_name + "?", operation);
    }
    userProfit.onExtractLog = function (data) {
        admin.putTempData('formOk', false);
        layer.full(layui.admin.open({
            type: 2,
            title: '提现记录',
            area: ['100%', '100%'],
            content: Feng.ctxPath + '/exam/userProfit_extract_log?userId=' + data.user_id ,
            end: function () {
                admin.getTempData('formOk') && table.reload(userProfit.tableId);
            }
        }));
    }

    userProfit.onProfitLog = function (data) {
        admin.putTempData('formOk', false);
        layer.full(layui.admin.open({
            type: 2,
            title: '收益记录',
            area: ['100%', '100%'],
            content: Feng.ctxPath + '/exam/userProfit_log?userId=' + data.user_id ,
            end: function () {
                admin.getTempData('formOk') && table.reload(userProfit.tableId);
            }
        }));
    }

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + userProfit.tableId,
        url: Feng.ctxPath + '/exam/userProfitList',
        page: true,
        height: "full-98",
        cellMinWidth: 120,
        cols: userProfit.initColumn()
    });


    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        userProfit.search();
    });

    // 导出excel
    $('#btnExp').click(function () {
        userProfit.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + userProfit.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'extract') {
            userProfit.onExtractExam(data);
        }else if (layEvent === 'transfer') {
            userProfit.onTransferExam(data);
        }else if (layEvent === 'userProfit_extract_log') {
            userProfit.onExtractLog(data);
        }else if (layEvent === 'userProfit_log') {
            userProfit.onProfitLog(data);
        }
    });

});
